package com.be.beadmin.material.util;

import com.be.beadmin.common.constant.JpaQueryConstant;
import com.be.beadmin.common.entity.FilderExpressEntity;
import com.be.beadmin.common.utils.JpaQueryBuilderNew;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author TANG
 */
public class JpaQueryBuilderJoin extends JpaQueryBuilderNew {
    public static <Z,X> Predicate getPredicateJoin(Join<Z,X> join, List<FilderExpressEntity> filderExpressEntitys, CriteriaBuilder cb) {
        List<Predicate> list = new ArrayList();
        if (filderExpressEntitys == null) {
            return cb.and((Predicate[])list.toArray(new Predicate[0]));
        } else {
            Predicate predicate = cb.conjunction();

            for(Integer i = 0; i < filderExpressEntitys.size(); i = i + 1) {
                FilderExpressEntity item = (FilderExpressEntity)filderExpressEntitys.get(i);
                Object item_value = item.getValue();
                String var9 = item.getOperator();
                byte var10 = -1;
                switch(var9.hashCode()) {
                    case 3244:
                        if (var9.equals("eq")) {
                            var10 = 0;
                        }
                        break;
                    case 3309:
                        if (var9.equals("gt")) {
                            var10 = 2;
                        }
                        break;
                    case 3365:
                        if (var9.equals("in")) {
                            var10 = 7;
                        }
                        break;
                    case 3464:
                        if (var9.equals("lt")) {
                            var10 = 3;
                        }
                        break;
                    case 102680:
                        if (var9.equals("gte")) {
                            var10 = 1;
                        }
                        break;
                    case 107485:
                        if (var9.equals("lte")) {
                            var10 = 4;
                        }
                        break;
                    case 3034793:
                        if (var9.equals("btwn")) {
                            var10 = 6;
                        }
                        break;
                    case 3321751:
                        if (var9.equals("like")) {
                            var10 = 5;
                        }
                    default:

                }

                Map dateMap;
                switch(var10) {
                    case 0:
                        if (i == 0) {
                            predicate = cb.equal(join.get(item.getField()), item_value);
                        } else if (item.getAndOr().equals(JpaQueryConstant.OR)) {
                            predicate = cb.or((Expression)predicate, cb.equal(join.get(item.getField()), item_value));
                        } else if (item.getAndOr().equals(JpaQueryConstant.AND)) {
                            predicate = cb.and((Expression)predicate, cb.equal(join.get(item.getField()), item_value));
                        }
                        break;
                    case 1:
                        dateMap = dateStrIsValid(String.valueOf(item_value), "yyyy-MM-dd");
                        if (dateMap.get("result").equals(true)) {
                            item_value = dateMap.get("date");
                        }

                        if (i == 0) {
                            predicate = cb.greaterThanOrEqualTo(join.get(item.getField()), (Comparable)item_value);
                        } else if (item.getAndOr().equals(JpaQueryConstant.OR)) {
                            predicate = cb.or((Expression)predicate, cb.greaterThanOrEqualTo(join.get(item.getField()), (Comparable)item_value));
                        } else if (item.getAndOr().equals(JpaQueryConstant.AND)) {
                            predicate = cb.and((Expression)predicate, cb.greaterThanOrEqualTo(join.get(item.getField()), (Comparable)item_value));
                        }
                        break;
                    case 2:
                        dateMap = dateStrIsValid(String.valueOf(item_value), "yyyy-MM-dd");
                        if (dateMap.get("result").equals(true)) {
                            item_value = dateMap.get("date");
                        }

                        if (i == 0) {
                            predicate = cb.greaterThan(join.get(item.getField()), (Comparable)item_value);
                        } else if (item.getAndOr().equals(JpaQueryConstant.OR)) {
                            predicate = cb.or((Expression)predicate, cb.greaterThan(join.get(item.getField()), (Comparable)item_value));
                        } else if (item.getAndOr().equals(JpaQueryConstant.AND)) {
                            predicate = cb.and((Expression)predicate, cb.greaterThan(join.get(item.getField()), (Comparable)item_value));
                        }
                        break;
                    case 3:
                        dateMap = dateStrIsValid(String.valueOf(item_value), "yyyy-MM-dd");
                        if (dateMap.get("result").equals(true)) {
                            item_value = dateMap.get("date");
                        }

                        if (i == 0) {
                            predicate = cb.lessThan(join.get(item.getField()), (Comparable)item_value);
                        } else if (item.getAndOr().equals(JpaQueryConstant.OR)) {
                            predicate = cb.or((Expression)predicate, cb.lessThan(join.get(item.getField()), (Comparable)item_value));
                        } else if (item.getAndOr().equals(JpaQueryConstant.AND)) {
                            predicate = cb.and((Expression)predicate, cb.lessThan(join.get(item.getField()), (Comparable)item_value));
                        }
                        break;
                    case 4:
                        dateMap = dateStrIsValid(String.valueOf(item_value), "yyyy-MM-dd");
                        if (dateMap.get("result").equals(true)) {
                            item_value = dateMap.get("date");
                        }

                        if (i == 0) {
                            predicate = cb.lessThanOrEqualTo(join.get(item.getField()), (Comparable)item_value);
                        } else if (item.getAndOr().equals(JpaQueryConstant.OR)) {
                            predicate = cb.or((Expression)predicate, cb.lessThanOrEqualTo(join.get(item.getField()), (Comparable)item_value));
                        } else if (item.getAndOr().equals(JpaQueryConstant.AND)) {
                            predicate = cb.and((Expression)predicate, cb.lessThanOrEqualTo(join.get(item.getField()), (Comparable)item_value));
                        }
                        break;
                    case 5:
                        if (i == 0) {
                            predicate = cb.like(join.get(item.getField()), "%" + String.valueOf(item_value) + "%");
                        } else if (item.getAndOr().equals(JpaQueryConstant.OR)) {
                            predicate = cb.or((Expression)predicate, cb.like(join.get(item.getField()), "%" + String.valueOf(item_value) + "%"));
                        } else if (item.getAndOr().equals(JpaQueryConstant.AND)) {
                            predicate = cb.and((Expression)predicate, cb.like(join.get(item.getField()), "%" + String.valueOf(item_value) + "%"));
                        }
                        break;
                    case 6:
                        List<Object> between_item = new ArrayList((List)item_value);
                        List<Object> between = new ArrayList();
                        Iterator var13 = between_item.iterator();

                        while(var13.hasNext()) {
                            Object btwn = var13.next();
                            dateMap = dateStrIsValid(String.valueOf(btwn), "yyyy-MM-dd");
                            if (dateMap.get("result").equals(true)) {
                                between.add(dateMap.get("date"));
                            } else {
                                between.add(btwn);
                            }
                        }

                        if (i == 0) {
                            predicate = cb.between(join.get(item.getField()), (Comparable)between.get(0), (Comparable)between.get(1));
                        } else if (item.getAndOr().equals(JpaQueryConstant.OR)) {
                            predicate = cb.or((Expression)predicate, cb.between(join.get(item.getField()), (Comparable)between.get(0), (Comparable)between.get(1)));
                        } else if (item.getAndOr().equals(JpaQueryConstant.AND)) {
                            predicate = cb.and((Expression)predicate, cb.between(join.get(item.getField()), (Comparable)between.get(0), (Comparable)between.get(1)));
                        }
                        break;
                    case 7:
                        if (i == 0) {
                            predicate = cb.in(join.get(item.getField())).value(item_value);
                        } else if (item.getAndOr().equals(JpaQueryConstant.OR)) {
                            predicate = cb.or((Expression)predicate, cb.in(join.get(item.getField())).value(item_value));
                        } else if (item.getAndOr().equals(JpaQueryConstant.AND)) {
                            predicate = cb.and((Expression)predicate, cb.in(join.get(item.getField())).value(item_value));
                        }
                }
            }

            return (Predicate)predicate;
        }
    }
    }
